package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.MissionArgument
import com.gin.kotlin.no_gay_alliance.model.MissionInfoModel
import com.gin.nga.main.NgaApi
import com.gin.nga.response.mission.MissionInfo
import java.lang.ref.WeakReference

class MissionInfoProvider private constructor(): AbstractCachedDataProvider<MissionArgument, List<List<MissionInfo>>, List<MissionInfoModel>>(
    javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, MissionInfoModel::class.java),
) {
    override val dir = "MISSION_INFO"

    override fun transform(rawData: List<List<MissionInfo>>) = rawData.flatten().map { MissionInfoModel(it) }

    override fun requestMethod(ngaApi: NgaApi, params: MissionArgument?) = ngaApi.missionApi.list(
        event = params!!.event.id,
        type = params.type.toBit()
    )

    override fun obtainRelativePath(userId: Long, params: MissionArgument?) = "${userId}_${params?.event}_${params?.type}.json"

    companion object {
        private var instance: WeakReference<MissionInfoProvider> = WeakReference(MissionInfoProvider())
        fun getInstance(): MissionInfoProvider = instance.get() ?: MissionInfoProvider().also { instance = WeakReference(it) }
    }
}